package com.example.application1.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.application1.API.pressApi;
import com.example.application1.Adapter.expressAdapter;
import com.example.application1.Entity.ListDTO;
import com.example.application1.Entity.NewslistDTOX;
import com.example.application1.Entity.pressBean;
import com.example.application1.R;
import com.example.application1.tools.DividerGridItemDecoration;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class expressFragment extends Fragment {
    private View view;
    private EditText edit_expressNumbers;
    private Button expressSubmit;
    private List<NewslistDTOX> expressList;
    private List<ListDTO> list;
    private TextView express_status;
    private TextView express_updateTime;
    private TextView express_company;
    private TextView express_phone;
    private RecyclerView ExpressRecycleView;
    private String numbers="75467914056735";

    public static expressFragment newInstance() {
        expressFragment fragment = new expressFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      view=inflater.inflate(R.layout.expressfind,container,false);
      return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initView();


//        System.out.println(expressList.get(0).getKuaidiname());
//        System.out.println(list.get(0).getContent());
        requestExpressData(numbers);
        useAdapterInitView();
    }



    public void initView(){
      edit_expressNumbers=view.findViewById(R.id.edit_expressNumbers);
      expressSubmit=view.findViewById(R.id.expressSubmit);
      express_status=view.findViewById(R.id.express_status);
      express_updateTime=view.findViewById(R.id.express_updateTime);
      express_company=view.findViewById(R.id.express_company);
      express_phone=view.findViewById(R.id.express_phone);
      ExpressRecycleView=view.findViewById(R.id.ExpressRecycleView);
        expressSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestExpressData(edit_expressNumbers.getText().toString());
            }
        });
    }

    public void setTextView(){
//        -1待查询、0查询异常、1暂无记录、2在途中、
//        3 派送中、4已签收、5用户拒签、6疑难件、
//        7无效单、8超时单、9签收失败、10退回
        express_phone.setText(expressList.get(0).getTelephone());
        express_company.setText(expressList.get(0).getKuaidiname());
        express_updateTime.setText(expressList.get(0).getUpdatetime());
        switch (expressList.get(0).getStatus()){
            case -1:express_status.setText("待查询");
                break;
            case 0:express_status.setText("查询异常");
                break;
            case 1:express_status.setText("暂无记录");
                break;
            case 2:express_status.setText("在途中");
                break;
            case 3:express_status.setText("派送中");
                break;
            case 4:express_status.setText("已签收");
                break;
            case 5:express_status.setText("用户拒签");
                break;
            case 6:express_status.setText("疑难件");
                break;
            case 7:express_status.setText("无效单");
                break;
            case 8:express_status.setText("超时单");
                break;
            case 9:express_status.setText("签收失败");
                break;
            case 10:express_status.setText("退回");
                break;
            default:express_status.setText("没有订单");
                break;
        }
    }

    public void useAdapterInitView(){
        ExpressRecycleView.setLayoutManager(new LinearLayoutManager(getContext()));
        expressAdapter adapter=new expressAdapter(getContext(),list);
        ExpressRecycleView.setAdapter(adapter);
        ExpressRecycleView.addItemDecoration(new DividerGridItemDecoration(getContext()));
        adapter.refresh(list);
    }

    public void requestExpressData(String numbers){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://api.tianapi.com/txapi/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        pressApi service = retrofit.create(pressApi.class);
        Call<pressBean> call = service.getCall("1fe590b621dcb1472141d1dee8323c85",numbers);
        call.enqueue(new Callback<pressBean>() {
            @Override
            public void onResponse(Call<pressBean> call, Response<pressBean> response) {
                expressList= response.body().getNewslist();
                list=response.body().getNewslist().get(0).getList();
            }
            @Override
            public void onFailure(Call<pressBean> call, Throwable t) {
                System.out.println("连接失败");
            }
        });

    }

}
